package grails3.security.druid;

import com.alibaba.druid.pool.DruidDataSource;
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * @Author Lee
 * @Description
 * @Date 2019年07月11日 22:26
 */
public class DruidConnectionProvider extends com.alibaba.druid.support.hibernate.DruidConnectionProvider {

    //Druid连接池
    private DruidDataSource dataSource;

    public Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }

    @Override
    public void closeConnection(Connection conn) throws SQLException {
        conn.close();
    }

    @Override
    public boolean supportsAggressiveRelease() {
        return false;
    }

    public void shutdown() throws SQLException {
        dataSource.close();
    }

    public DruidConnectionProvider(DruidDataSource dataSource) {
        System.out.println("初始化。。。");
        dataSource = dataSource;
    }

    public DruidDataSource getDataSource() {
        return dataSource;
    }

    public void setDataSource(DruidDataSource dataSource) {
        this.dataSource = dataSource;
    }

    public boolean isUnwrappableAs(Class unwrapType) {
        return this.dataSource.isWrapperFor(unwrapType);
    }

    @Override
    public <T> T unwrap(Class<T> unwrapType) {
        return this.dataSource.unwrap(unwrapType);
    }
}
